package com.sgu6.sgu6_machine.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.sgu6.sgu6_machine.R;

import java.util.List;

/**
 * 后台功能使用说明适配器
 */
public class InstructionAdapter extends BaseExpandableListAdapter {

    private Context mContext;
    private List<String> group;
    private List<List<String>> child;
    private LayoutInflater mInflater;

    public InstructionAdapter(Context mContext, List<String> group, List<List<String>> child) {
        this.mContext = mContext;
        this.mInflater = LayoutInflater.from(mContext);
        this.group = group;
        this.child = child;
    }


    @Override
    public int getGroupCount() {
        return group.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return child.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return group.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return child.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder groupViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_instruction_group, parent, false);
            groupViewHolder = new GroupViewHolder();
            groupViewHolder.tvLabel = (TextView) convertView.findViewById(R.id.tv_label);
            convertView.setTag(groupViewHolder);

        } else {
            groupViewHolder = (GroupViewHolder) convertView.getTag();
        }
        groupViewHolder.tvLabel.setText(group.get(groupPosition));
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder childViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_instruction_child, parent, false);
            childViewHolder = new ChildViewHolder();
            childViewHolder.tvInstruction = (TextView) convertView.findViewById(R.id.tv_instruction);
            convertView.setTag(childViewHolder);

        } else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }
        childViewHolder.tvInstruction.setText(child.get(groupPosition).get(childPosition));
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    static class GroupViewHolder {
        TextView tvLabel;
    }

    static class ChildViewHolder {
        TextView tvInstruction;

    }
}
